/** https://blog.csdn.net/weixin_42912702/article/details/126362406 */
function ajaxRequest(url: string | URL, options: { method?: any; headers?: any; onReqProgress?: any; onResProgress?: any; credentials?: any; responseType?: any; body?: any; parseResponse?: any; timeout?: any; cancelToken?: any; signal?: any; }) {
    return new Promise(function (resolve, reject) {
        var _a, _b, _c;
        const method = (_a = options.method) != null ? _a : 'get';
        const headers = (_b = options.headers) != null ? _b : {};
        const {
            onReqProgress,
            onResProgress,
            credentials,
            responseType,
            body,
            parseResponse,
            timeout,
            cancelToken,
            signal,
        } = options;
        const xhr = new XMLHttpRequest();
        if (timeout != null) {
            xhr.timeout = timeout;
        }
        switch (credentials) {
            case 'include': {
                xhr.withCredentials = true;
                break;
            }
            case 'omit': {
                xhr.withCredentials = false;
                break;
            }
        }
        if (responseType !== 'formData') {
            xhr.responseType =
                (_c = responseType == null ? void 0 : responseType.toLowerCase()) != null ? _c : 'json';
        }
        xhr.open(method, url);
        let headersEntries = [];
        if (Array.isArray(headers)) {
            headersEntries = headers;
        } else if (headers instanceof Headers) {
            headers.forEach((value, key) => {
                headersEntries.push([key, value]);
            });
        } else {
            headersEntries = Object.entries(headers);
        }
        for (const [key, value] of headersEntries) {
            xhr.setRequestHeader(key, value);
        }
        if (cancelToken) {
            cancelToken.promise.then(function () {
                xhr.abort();
            });
        }
        if (signal) {
            signal.addEventListener(
                'abort',
                function () {
                    xhr.abort();
                },
                {
                    once: true,
                },
            );
        }
        xhr.onload = () => {
            // resolve(parseResponse === false ? xhr.responseText : xhr.response);
            if (xhr.status >= 200 && xhr.status < 300) {
                resolve(parseResponse === false ? xhr.responseText : xhr.response);
            } else {
                const myBlob = new Blob();
                const myOptions = {
                    status: xhr.status,
                    statusText: 'SuperSmashingGreat!',
                };
                //创建 Response实例
                const myResponse = new Response(myBlob, myOptions);
                resolve(myResponse);
            }
        };
        xhr.onerror = reject;
        if (xhr.upload && onReqProgress) {
            xhr.upload.onprogress = onReqProgress;
        }
        if (onResProgress) {
            xhr.onprogress = onResProgress;
        }
        xhr.send(body);
    });
}
const progressMiddleware = async function (ctx: { req: { url: any; options: any; }; res: unknown; }, next: () => any) {
    const { url, options } = ctx.req;
    if (!(options.onReqProgress || options.onResProgress)) {
        await next();
        return;
    }
    //如需执行响应拦截 不可修改 __umiRequestCoreType__
    // options.__umiRequestCoreType__ = "ajaxRequest";
    const response = await ajaxRequest(url, options);
    ctx.res = response;
    await next();
};
export { progressMiddleware as default };

